Page move undo feature
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
index dbba17d..fc52580 100644 (file)
@@ -17,7 +17,7 @@ function wfSpecialMovepage() {
        global $wgUser, $wgOut, $wgRequest, $action, $wgOnlySysopMayMove;
 
        # check rights. We don't want newbies to move pages to prevents possible attack
-       if ( 0 == $wgUser->getID() or $wgUser->isBlocked() or ($wgOnlySysopMayMove and $wgUser->isNewbie())) {
+       if ( $wgUser->isAnon() or $wgUser->isBlocked() or ($wgOnlySysopMayMove and $wgUser->isNewbie())) {
                $wgOut->errorpage( "movenologin", "movenologintext" );
                return;
        }
@@ -29,9 +29,14 @@ function wfSpecialMovepage() {
 
        $f = new MovePageForm();
 
-       if ( 'success' == $action ) { $f->showSuccess(); }
-       else if ( 'submit' == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
-       else { $f->showForm( '' ); }
+       if ( 'success' == $action ) {
+               $f->showSuccess();
+       } else if ( 'submit' == $action && $wgRequest->wasPosted()
+               && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
+               $f->doSubmit();
+       } else {
+               $f->showForm( '' );
+       }
 }
 
 /**
@@ -40,12 +45,15 @@ function wfSpecialMovepage() {
  * @subpackage SpecialPage
  */
 class MovePageForm {
-       var $oldTitle, $newTitle; # Text input
+       var $oldTitle, $newTitle, $reason; # Text input
+       var $moveTalk;
                
        function MovePageForm() {
                global $wgRequest;
                $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $wgRequest->getVal( 'target' ) );
                $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
+               $this->reason = $wgRequest->getText( 'wpReason' );
+               $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
        }
        
        function showForm( $err ) {
@@ -53,53 +61,76 @@ class MovePageForm {
 
                $wgOut->setPagetitle( wfMsg( 'movepage' ) );
 
-               if ( empty( $this->oldTitle ) ) {
+               if ( $this->oldTitle == '' ) {
                        $wgOut->errorpage( 'notargettitle', 'notargettext' );
                        return;
                }
+
+               $ot = Title::newFromURL( $this->oldTitle );
+               $oldTitle = $ot->getPrefixedText();
                
                $encOldTitle = htmlspecialchars( $this->oldTitle );
-               $encNewTitle = htmlspecialchars( $this->newTitle );
-               $ot = Title::newFromURL( $this->oldTitle );
-               $ott = $ot->getPrefixedText();
+               if( $this->newTitle == '' ) {
+                       # Show the current title as a default
+                       # when the form is first opened.
+                       $encNewTitle = $oldTitle;
+               } else {
+                       $encNewTitle = htmlspecialchars( $this->newTitle );
+               }
+               $encReason = htmlspecialchars( $this->reason );
 
                $wgOut->addWikiText( wfMsg( 'movepagetext' ) );
-               if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
+               if ( !$ot->isTalkPage() ) {
                        $wgOut->addWikiText( wfMsg( 'movepagetalktext' ) );
                }
 
-               $ma = wfMsg( 'movearticle' );
-               $newt = wfMsg( 'newtitle' );
-               $mpb = wfMsg( 'movepagebtn' );
+               $movearticle = wfMsg( 'movearticle' );
+               $newtitle = wfMsg( 'newtitle' );
+               $movepagebtn = wfMsg( 'movepagebtn' );
                $movetalk = wfMsg( 'movetalk' );
+               $movereason = wfMsg( 'movereason' );
 
                $titleObj = Title::makeTitle( NS_SPECIAL, 'Movepage' );
                $action = $titleObj->escapeLocalURL( 'action=submit' );
+               $token = htmlspecialchars( $wgUser->editToken() );
 
                if ( $err != '' ) {
                        $wgOut->setSubtitle( wfMsg( 'formerror' ) );
                        $wgOut->addHTML( '<p class="error">'.$err."</p>\n" );
                }
+
+               if ( $this->moveTalk ) {
+                       $moveTalkChecked = " checked='checked'";
+               } else {
+                       $moveTalkChecked = '';
+               }
+               
                $wgOut->addHTML( "
 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
        <table border='0'>
                <tr>
-                       <td align='right'>{$ma}:</td>
-                       <td align='left'><strong>{$ott}</strong></td>
+                       <td align='right'>{$movearticle}:</td>
+                       <td align='left'><strong>{$oldTitle}</strong></td>
                </tr>
                <tr>
-                       <td align='right'>{$newt}:</td>
+                       <td align='right'>{$newtitle}:</td>
                        <td align='left'>
                                <input type='text' size='40' name=\"wpNewTitle\" value=\"{$encNewTitle}\" />
                                <input type='hidden' name=\"wpOldTitle\" value=\"{$encOldTitle}\" />
                        </td>
+               </tr>
+               <tr>
+                       <td align='right'>{$movereason}:</td>
+                       <td align='left'>
+                               <input type='text' size=40 name=\"wpReason\" value=\"{$encReason}\" />
+                       </td>
                </tr>" );
 
-               if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
+               if ( ! $ot->isTalkPage() ) {
                        $wgOut->addHTML( "
                <tr>
                        <td align='right'>
-                               <input type='checkbox' name=\"wpMovetalk\" checked='checked' value=\"1\" />
+                               <input type='checkbox' name=\"wpMovetalk\"{$moveTalkChecked} value=\"1\" />
                        </td>
                        <td>{$movetalk}</td>
                </tr>" );
@@ -108,10 +139,11 @@ class MovePageForm {
                <tr>
                        <td>&nbsp;</td>
                        <td align='left'>
-                               <input type='submit' name=\"wpMove\" value=\"{$mpb}\" />
+                               <input type='submit' name=\"wpMove\" value=\"{$movepagebtn}\" />
                        </td>
                </tr>
        </table>
+       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
 </form>\n" );
 
        }
@@ -125,7 +157,6 @@ class MovePageForm {
                # Variables beginning with 'o' for old article 'n' for new article
 
                # Attempt to move the article
-               
                $ot = Title::newFromText( $this->oldTitle );
                $nt = Title::newFromText( $this->newTitle );
 
@@ -135,12 +166,12 @@ class MovePageForm {
                        return;
                }
 
-               $error = $ot->moveTo( $nt );
+               $error = $ot->moveTo( $nt, true, $this->reason );
                if ( $error !== true ) {
                        $this->showForm( wfMsg( $error ) );
                        return;
                }
-               
+
                # Update counters if the article got moved into or out of NS_MAIN namespace
                $ons = $ot->getNamespace();
                $nns = $nt->getNamespace();
@@ -165,7 +196,6 @@ class MovePageForm {
                # (1) the checkbox says to,
                # (2) the namespaces are not themselves talk namespaces, and of course
                # (3) it exists.
-               
                if ( ( $wgRequest->getVal('wpMovetalk') == 1 ) &&
                     ( ! Namespace::isTalk( $ons ) ) &&
                     ( ! Namespace::isTalk( $nns ) ) ) {
@@ -180,7 +210,7 @@ class MovePageForm {
                        $ntt = Title::makeTitle( $nns, $nt->getDBkey() );
 
                        # Attempt the move
-                       $error = $ott->moveTo( $ntt );
+                       $error = $ott->moveTo( $ntt, true, $this->reason );
                        if ( $error === true ) {
                                $talkmoved = 1;
                        } else {
@@ -189,7 +219,6 @@ class MovePageForm {
                }
                
                # Give back result to user.
-               
                $titleObj = Title::makeTitle( NS_SPECIAL, 'Movepage' );
                $success = $titleObj->getFullURL( 
                  'action=success&oldtitle=' . wfUrlencode( $ot->getPrefixedText() ) .
@@ -200,7 +229,7 @@ class MovePageForm {
        }
 
        function showSuccess() {
-               global $wgOut, $wgUser, $wgRequest;
+               global $wgOut, $wgRequest, $wgRawHtml;
 
                $wgOut->setPagetitle( wfMsg( 'movepage' ) );
                $wgOut->setSubtitle( wfMsg( 'pagemovedsub' ) );
@@ -209,6 +238,8 @@ class MovePageForm {
                $talkmoved = $wgRequest->getVal('talkmoved');
 
                $text = wfMsg( 'pagemovedtext', $oldtitle, $newtitle );
+               
+               # Temporarily disable raw html wikitext option out of XSS paranoia
                $marchingantofdoom = $wgRawHtml;
                $wgRawHtml = false;
                $wgOut->addWikiText( $text );
@@ -220,7 +251,7 @@ class MovePageForm {
                        $wgOut->addHTML( "\n<p><strong>" . wfMsg( 'talkexists' ) . "</strong></p>\n" );
                } else {
                        $ot = Title::newFromURL( $oldtitle );
-                       if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
+                       if ( ! $ot->isTalkPage() ) {
                                $wgOut->addHTML( "\n<p>" . wfMsg( 'talkpagenotmoved', wfMsg( $talkmoved ) ) . "</p>\n" );
                        }
                }